package monster.com.cvh.fragment;

import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.bean.ResultBean;
import monster.com.cvh.bean.SelfEvaluationBean;
import monster.com.cvh.event.RefreshCompletedEvent;
import monster.com.cvh.fragment.base.ResumeInfoBaseFragment;
import monster.com.cvh.util.KeyboardUtils;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.SPUtils;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: 13580
 * @data: 2018/3/27
 * @function:资格证书Fragment
 */

public class SelfEvaluationFragment extends ResumeInfoBaseFragment {
    private static final String TAG = "SelfEvaluationFragment";
    @BindView(R.id.tv_include_top_bar_title)
    TextView mTVTitle;
    @BindView(R.id.edt_fragment_self_evaluation_content)
    EditText mEdtContent;
    @BindView(R.id.tv_fragment_self_evaluation_content)
    TextView mEdtContentCount;
    private String mSelfIntro;

    public static SelfEvaluationFragment newInstance() {
        SelfEvaluationFragment fragment = new SelfEvaluationFragment();
        return fragment;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_self_evaluation;
    }

    @Override
    protected void initData() {
        OkGo.<String>get(MyConstant.GET_RESUME_SELF_INTRO).tag(this)
                .params(MyConstant.TOKEN, SPUtils.getString(getContext(), MyConstant.TOKEN, ""))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e(TAG, "获取自我介绍: " + s);
                        try {
                            SelfEvaluationBean bean = new Gson().fromJson(s, SelfEvaluationBean.class);
                            if (bean != null && bean.getCode() == 1) {
                                mSelfIntro = bean.getData().getSelf_intro();
                                if (mSelfIntro != null) {
                                    mEdtContent.setText(mSelfIntro);
                                    mEdtContent.setSelection(mSelfIntro.length());
                                }
                            } else {
                                showToastShort(R.string.net_load_failed);
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            showToastShort(R.string.net_load_failed);
                        } finally {
                            mDialog.dismiss();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        mDialog.dismiss();
                        showToastShort(R.string.net_load_failed);
                    }
                });

    }

    @Override
    protected void initEvent() {
        mTVTitle.setText(getResources().getString(R.string.fragment_self_evaluation_title));
        mEdtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mEdtContentCount.setText(String.valueOf(s.length()));
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
    }

    @OnClick({R.id.iv_include_top_bar_back, R.id.tv_include_top_bar_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_include_top_bar_back:
                isSaveOnExit();
                break;
            case R.id.tv_include_top_bar_save:
                if (!isDataChanged()) {
                    saveInfoData();
                } else {
                    getActivity().finish();
                }
                break;
        }
    }

    private void isSaveOnExit() {
        if (!isDataChanged()) {
            mAlertDialog.show();
        } else {
            KeyboardUtils.hideSoftInput((Activity) mContext);
            ((Activity)mContext).finish();
        }
    }

    @Override
    protected void saveInfoData() {
        mDialog.show();
        OkGo.<String>put(MyConstant.MODIFY_RESUME_SELF_INTRO).tag(this)
                .params(MyConstant.TOKEN, SPUtils.getString(getContext(), MyConstant.TOKEN, ""))
                .params(MyConstant.SELF_INTRO, mEdtContent.getText().toString())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.e(TAG, "修改自我介绍: " + s);
                        try {
                            ResultBean resultBean = new Gson().fromJson(s, ResultBean.class);
                            if (resultBean.getCode() == 1) {
                                showToastShort(R.string.net_save_success);
                                //通知刷新完成度
                                RefreshCompletedEvent.getInstance().onRefreshCompleted();
                                getActivity().finish();
                            } else {
                                showToastShort(R.string.net_load_failed);
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                            showToastShort(R.string.net_load_failed);
                        } finally {
                            mDialog.dismiss();
                            KeyboardUtils.hideSoftInput((Activity) mContext);
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        showToastShort(R.string.net_load_failed);
                        mDialog.dismiss();
                    }
                });
    }

    @Override
    public boolean isDataChanged() {
        if (mSelfIntro != null) {
            return mSelfIntro.equals(mEdtContent.getText().toString());
        } else {
            return true;
        }
    }

    @Override
    public void onExit() {
        isSaveOnExit();
    }


}
